package net.airtheva.uniqueTask4.fragment;

import net.airtheva.uniqueTask4.MainActivity;
import net.airtheva.uniqueTask4.Playitem;
import net.airtheva.uniqueTask4.R;
import net.airtheva.uniqueTask4.Utils;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class StoreFragment extends Fragment {

	public static final String TAG = "StoreFragment";

	public static final String KEY_ARTIST = "artist";
	public static final String KEY_ALBUM = "album";
	public static final String KEY_TITLE = "title";
	public static final String KEY_URI = "_data";

	class _OnItemClickListener implements OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub

			Cursor c = (Cursor) mAdapter.getItem(position);

			Log.d(TAG, Utils.Join(c.getColumnNames(), ", "));

			AddAndPlay(c);

		}

	}

	class _OnMultiChoiceModeListener implements
			AbsListView.MultiChoiceModeListener {

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			// TODO Auto-generated method stub
			getActivity().getMenuInflater().inflate(R.menu.store_list_view,
					menu);
			return true;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void onItemCheckedStateChanged(ActionMode mode, int position,
				long id, boolean checked) {
			// TODO Auto-generated method stub

		}

	}

	ListView mStoreListView;

	Cursor mCursor;
	CursorAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mStoreListView = new ListView(getActivity());

		mStoreListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);

		mStoreListView.setOnItemClickListener(new _OnItemClickListener());
		mStoreListView
				.setMultiChoiceModeListener(new _OnMultiChoiceModeListener());

		return mStoreListView;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Setup();
	}

	void Setup() {

		String[] from = new String[] { KEY_ARTIST, KEY_ALBUM, KEY_TITLE };

		int[] to = new int[] { R.id.store_list_view_item_artist,
				R.id.store_list_view_item_album,
				R.id.store_list_view_item_title };

		mCursor = getActivity().getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				null);
		mAdapter = new SimpleCursorAdapter(getActivity(),
				R.layout.store_list_view_item, mCursor, from, to,
				CursorAdapter.FLAG_AUTO_REQUERY);

		mStoreListView.setAdapter(mAdapter);

	}

	public void AddAndPlay(Cursor cursor) {

		String artist = cursor.getString(cursor.getColumnIndex(KEY_ARTIST));
		String album = cursor.getString(cursor.getColumnIndex(KEY_ALBUM));
		String title = cursor.getString(cursor.getColumnIndex(KEY_TITLE));
		String uri = cursor.getString(cursor.getColumnIndex(KEY_URI));
		
		

		Playitem playitem = new Playitem();
		playitem.Artist = artist;
		playitem.Album = album;
		playitem.Title = title;
		playitem.Uri = uri;

		MainActivity mainActivity = (MainActivity) getActivity();
		mainActivity.PlaylistFragment.AddAndPlay(playitem);

	}

}
